package com.uxwine.alarm;


import com.uxwine.alarm.db.TBAlram;
import com.uxwine.alarm.mon.RingManager;

import android.database.Cursor;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.BaseColumns;

public class Alarm implements Parcelable, TBAlram{
	
	public int id;
	public boolean enabled;
	public TimeRecord time;
	public TimeCycle cycle;
	public long trigger;
	public boolean vibrate;
	public String label;
	public Uri alert;
	public boolean silent;
	
	public Alarm()
	{
		id = -1;
		time = new TimeRecord(System.currentTimeMillis());
		vibrate = true;
		cycle = new TimeCycle(0);
		alert = RingManager.getDefaultRing();
	}

	public Alarm(Parcel src)
	{
		id = src.readInt();
		enabled = src.readInt() == 1;
		int[] values = new int[10];
		src.readIntArray(values);
		time = new TimeRecord(values);
		cycle = new TimeCycle(src.readLong());
		trigger = src.readLong();
		vibrate = src.readInt() == 1;
		label = src.readString();
		alert = (Uri)src.readParcelable(null);
		silent = src.readInt() == 1;
	}
	
	public Alarm(Cursor c)
	{
		id = c.getInt(Columns.INDEX_ID);
		enabled = c.getInt(Columns.INDEX_ENABLE) == 1;
		time = new TimeRecord(c.getString(Columns.INDEX_TIME));
		cycle = new TimeCycle(c.getLong(Columns.INDEX_CYCLE));
		trigger = c.getLong(Columns.INDEX_TRIGGER);
		vibrate = c.getInt(Columns.INDEX_VIBRATE) == 1;
		label = c.getString(Columns.INDEX_LABEL);
		String as = c.getString(Columns.INDEX_ALERT);
		if(AlarmData.ALARM_ALERT_SILENT.equals(as))
		{
			silent = true;
		}
		else
		{
			if(as != null && as.length() >= 0)
				alert = Uri.parse(as);
			if(null == alert)
				alert = RingManager.getDefaultRing();
		}
	}
	
	public void copy(Alarm a)
	{
		id = a.id;
		enabled = a.enabled;
		time = a.time;
		cycle = a.cycle;
		trigger = a.trigger;
		vibrate = a.vibrate;
		label = a.label;
		silent = a.silent;
		alert = a.alert;
	}
	
	//$========================================================
	//@author 	: zhangzhongming<QQ:911696301>
	//@date		: 2013.4.28	
	//序列化部分
	//TAG		:start
	//=========================================================
	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(id);
		dest.writeInt(enabled ? 1 : 0);
		dest.writeIntArray(time.getValues());
		dest.writeLong(cycle.getCycle());
		dest.writeLong(trigger);
		dest.writeInt(vibrate ? 1 : 0);
		dest.writeString(label);
		dest.writeParcelable(alert, flags);
		dest.writeInt(silent ? 1 : 0);
	}
	
	 public static final Parcelable.Creator<Alarm> CREATOR
     	= new Parcelable.Creator<Alarm>()
     {
         public Alarm createFromParcel(Parcel p)
         {
             return new Alarm(p);
         }

         public Alarm[] newArray(int size) 
         {
             return new Alarm[size];
         }
     };
	//TAG		:end
	//$========================================================
}
